package com.foo.web.core.config;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class UploadUtil {
	
	public static List<UploadFile> getUploadFiles(
            MultipartHttpServletRequest multipartHttpServletRequest, String Path,String type) {
        List<MultipartFile> files = multipartHttpServletRequest
                .getFiles("file");
        List<UploadFile> fileList = new ArrayList<UploadFile>();
        for (MultipartFile file : files) {
            // 取得上传文件
            String fileName = file.getOriginalFilename();
            Long fileSize = file.getSize();
            if (null != fileName && !"".equals(fileName)) {
                try {
                    // 创建文件要保存的路径
                    File uploadFile = new File(Path);
                    if (!uploadFile.exists() || null == uploadFile) {
                        uploadFile.mkdirs();
                    }
                    // 获取文件类型
                    String fileType = fileName.substring(
                            fileName.lastIndexOf(".") + 1, fileName.length());
                    String id = UUID.randomUUID().toString();
                    String targetName = id + "." + type;
                    // 文件真实存放路径
                    String filePath = uploadFile.getPath() + File.separator
                            + targetName;
                    // 保存文件
                    file.transferTo(new File(filePath));
                    // 初始化上传文件
                    UploadFile up = new UploadFile(id, fileName, fileType,
                            fileSize + "", filePath);
                    fileList.add(up);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return fileList;
    }
	/**
	 * 修改尺寸
	 * @param newWidth
	 * @param newHeight
	 * @param path
	 * @return
	 */
	public static boolean changeSize(int newWidth, int newHeight, String path) {
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(path));

            //字节流转图片对象
            Image bi = ImageIO.read(in);
            //构建图片流
            BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            //绘制改变尺寸后的图
            tag.getGraphics().drawImage(bi, 0, 0, newWidth, newHeight, null);
            //输出流
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path));
            //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            //encoder.encode(tag);
            ImageIO.write(tag, "JPG", out);
            in.close();
            out.close();
            System.out.println("cheng");
            return true;
        } catch (IOException e) {
        	System.out.println(e);
            return false;
        }
	}
	 public static void filedel(String filedel) {
			File file = new File(filedel);
			if (!file.exists()) {
				System.out.println("文件不存在");
			} else {
				System.out.println("存在文件");
				file.delete();// 删除文件
			}
		}

}
